package com.zmn.mcc.admin.controller.common;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.admin.controller.role.RoleBaseController;
import com.zmn.mcc.business.interfaces.staff.StaffBService;
import com.zmn.mcc.common.constant.RoleDict;
import com.zmn.mcc.cache.DeptCache;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.role.McRoleQuery;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.services.interfaces.dept.DeptService;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/25 13:49
 */
@Controller
@RequestMapping("common/role/")
public class CommonRoleListController extends RoleBaseController {

    @Resource
    private DeptService deptService;
    @Resource
    private StaffService staffService;
    @Resource
    private StaffBService staffBService;
    @Resource
    private DeptCache deptCache;


    /**
     * 角色列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,            // 主ID，用于回调方法读写相关数据
                       @RequestParam(value = "callbackUrl", defaultValue = "") String callbackUrl,  // 回调方法的url
                       @RequestParam(value = "multi", defaultValue = "1") Integer multi,            // 是否允许多选，1不允许，2允许
                       @ModelAttribute McRoleQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setRoleId(Integer.parseInt(keyword));
            query.setRoleName("");
        } else {
            query.setRoleName(keyword);
        }

        // 如员工无"系统全局角色"，则只可见部门及下级部门拥有的角色
        List<Integer> deptIds = new ArrayList<>();

        // 员工是否拥有"系统全局角色"
        Integer staffId = getStaffId();
        boolean isGlobalAdmin = staffBService.isGlobalAdmin(staffId);
        if(!isGlobalAdmin) {
            McStaff staff = staffService.findByKey(staffId);
            if(NumberUtil.isNotNullOrZero(staff.getDeptId())) {
                deptIds = deptCache.listSuborDeptId(staff.getDeptId());
            }
            if(CollectionUtil.isNullOrEmpty(deptIds)) {
                deptIds.add(-1);
            }
        }
        query.setDeptIds(deptIds);

        List<McRole> items = roleService.listPageByQuery(query);

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);
        model.addAttribute("types", RoleDict.roleTypeMap());

        model.addAttribute("keyId", keyId);
        model.addAttribute("callbackUrl", callbackUrl);
        model.addAttribute("multi", multi);

        return "common/role/list";
    }
}
